<template>
  <div class="app-container">
    <div class="filter-container">
       
    
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号"  align="center" width="60" >
        <template slot-scope="scope">
          <span>  {{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.slUserByUid.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.slUserByUid.phone }}</span>
        </template>
      </el-table-column>
       <el-table-column label="项目名称"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.slProjectByPid.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件信息" min-width="200px"> 
        <template slot-scope="{row}">
          
            <div v-for="(item,itemIndex) in row.contractFile " :key="itemIndex">
          <img :src="item" alt="" style="width:50px;height:30px">
        </div>
        </template>
      </el-table-column>
    
     
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
            <el-tag v-if="row.status ==1" type="success" >通过</el-tag>
          <el-tag v-else-if="row.status ==2" type="danger"  >已拒绝</el-tag>
          <el-tag v-else  >待审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" min-width="200px"> 
        <template slot-scope="{row}">
             <span class="link-type" >{{ row.createtime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
           
            <!-- <router-link :to="{path:'/contract/contractFile',query:{id:row.id}}">
                <el-button type="primary" size="mini" >
                  查看合同文件
                </el-button>
            </router-link> -->
            <el-dropdown >
            <el-button type="primary" size="mini" v-if="row.status =='0'">
              审核<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item >
                      <el-button type="success" size="mini" @click="handleCommand(row)">
                  <!-- <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'ziliao'}}"> -->
                  通过
                      </el-button>
                <!-- </router-link> -->
                </el-dropdown-item>
                 <el-dropdown-item command="2">
                  <!-- <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'youshi'}}"> -->
                  <!-- 拒绝 -->
                    <el-button type="wail" size="mini" @click="handleCommand2(row)">
                  <!-- <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'ziliao'}}"> -->
                  拒绝
                      </el-button>
                <!-- </router-link> -->
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { GQL } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination


export default {
  components: { Pagination },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
       
      },
      showReviewer: false,
   
    }
  },
  created() {
     this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let where = {}
        if(this.listQuery.page != 1){
             where = { "first": this.listQuery.limit, "offset": this.listQuery.page*10-10, "orderBy": "STATUS_ASC"}
        }else{
         where = { "first": this.listQuery.limit, "offset": this.listQuery.page-1, "orderBy": "STATUS_ASC"}
        }
       
        const query = {
            "operationName": "allSlProjectContracts",
            "query": "query allSlProjectContracts($orderBy: [SlProjectContractsOrderBy!] = [PRIMARY_KEY_ASC],$first: Int,$offset: Int) { allSlProjectContracts( orderBy:$orderBy offset:$offset first:$first) { nodes { pid id createtime contractFile status uid slUserByUid { uid nickname phone } slProjectByPid { title pid } } totalCount } }",
            "variables":where
        }
       GQL(query).then(response => {
           response.data.allSlProjectContracts.nodes.forEach(element => {
               element.contractFile = JSON.parse( element.contractFile)
           });
        //    console.log('1----',response)
        this.list = response.data.allSlProjectContracts.nodes
        this.total = response.data.allSlProjectContracts.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCommand(row){
         const num = "1";
         let query = {
            "operationName": "updateSlProjectContractById",
            "query": "mutation updateSlProjectContractById($input: UpdateSlProjectContractByIdInput!) { updateSlProjectContractById(input: $input) { slProjectContract { id } } }",
            "variables": { "input": { "id": row.id, "slProjectContractPatch": { "status": num } } }
            
          }

          GQL(query).then((res) => {
               if(res.errors){
                  this.$notify({
                    title: '审核失败',
                    message: '审核项目失败',
                    type: 'error',
                    duration: 2000
                  })
              }else{
                //消息
                 let query2 = {
                    "operationName": "createSlUserMessage",
                    "query": "mutation createSlUserMessage($input: CreateSlUserMessageInput!) { createSlUserMessage(input: $input) { slUserMessage { id } } }",
                    "variables": { "input": { "slUserMessage": { "message": "您参与的“"+row.slProjectByPid.title+"”项目合同资料已审核通过.", "type": "1","uid":row.uid } } }
                    
                  }
                  GQL(query2).then((res) => {
                    this.$notify({
                      title: '审核成功',
                      message: '审核项目成功',
                      type: 'success',
                      duration: 2000
                    })
                    this.handleFilter()
                  })
                 
              }
       
          })
    },
     handleCommand2(row){
        const num = "2";
        let query = {
            "operationName": "updateSlProjectContractById",
            "query": "mutation updateSlProjectContractById($input: UpdateSlProjectContractByIdInput!) { updateSlProjectContractById(input: $input) { slProjectContract { id } } }",
           "variables": { "input": { "id": row.id, "slProjectContractPatch": { "status": num } } }
           
          }
          GQL(query).then((res) => {
               if(res.errors){
                  this.$notify({
                    title: '审核失败',
                    message: '审核项目失败',
                    type: 'error',
                    duration: 2000
                  })
              }else{

                 let query2 = {
                    "operationName": "createSlUserMessage",
                    "query": "mutation createSlUserMessage($input: CreateSlUserMessageInput!) { createSlUserMessage(input: $input) { slUserMessage { id } } }",
                    "variables": { "input": { "slUserMessage": { "message": "您的合同资料被拒绝，请重新提交.", "type": "1" } } }
                    
                  }
                  GQL(query2).then((res) => {
                     this.$notify({
                      title: '审核成功',
                      message: '资料已被拒绝',
                      type: 'success',
                      duration: 2000
                    })
                    this.handleFilter()
                  })
               
              }
       
          })
    },


  }
}
</script>
<style scoped>
.demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
